#######################################################
# Example of Experimental Framework and Process 
# Author: moonho.hwang@gmail.com
#   Date: Oct.19.2011
#######################################################
from XSY.DEVS.CoupledDEVS import *
from XSY.DEVS.SimEngine import SimEngine
from XSY.DEVS.VeriEngine import VeriEngine
from XSY.DEVS.examples.DEVSs.Transducer import Transducer
from XSY.DEVS.examples.SDEVSs.GeneratorFS import Generator
from XSY.DEVS.examples.STDEVSs.ProcessorST import Processor
from XSY.Set.Interval import TInterval
########################## Two classes of moving entities are considered ################################
class A:
   def __int__(self):
      pass 
   def __str__(self):
      return "A"

class B:
   def __int__(self):
      pass 
   def __str__(self):
      return "B"
########################################################################################################

######################################### Process Network ##############################################
#
# +--------------------+
# |          EF        |                        +--------------+
# | +-------+          |                        |    Process   |
# | |  Gen  >!o ------->!gen_out -------+------->?in           |
# | |       <?pull ----<?gen_pull <-----|---+---<out_signal    |
# | +-------+          |                |   ^   |              |
# |                    |                |   |   |              |
# | +-------+          |                |   |   |              |
# | | Tran  <?sys_in---<?sys_in <-------'   |   |              |
# | |       <?sys_out--<?sys_out<-----------+---<!out          |
# | +-------+          |                        |              |
# +--------------------+                        +--------------+
########################################################################################################
def makeExperimentFrame():
   cm = CoupledDEVS("EF")
   gen = Generator(n="Gen", bAuto = False, OutTable={A:.7, B:.3}, IntGenTimeRV=TInterval(2,5))  
   trans = Transducer("tr")
   cm.addComponents(gen, trans)
   cm.addInputPorts("?gen_pull", "?sys_in", "?sys_out")
   cm.addOutputPorts("!gen_out")
   cm.addCouples((gen.OP["!out"], cm.OP["!gen_out"]), (cm.IP["?gen_pull"], gen.IP["?pull"]),
                 (cm.IP["?sys_in"], trans.IP["?sys_in"]), (cm.IP["?sys_out"], trans.IP["?sys_out"]))
   return cm

def makeEF_ProcessNetwork():
   EF = makeExperimentFrame()
   proc = Processor("proc", 10, 30)

   cm = CoupledDEVS("sys") # try it out 
   cm.addComponents(EF, proc) 
   cm.addCouples( (EF.OP["!gen_out"], EF.IP["?sys_in"]), (EF.OP["!gen_out"], proc.IP["?in"]), 
                  (proc.OP["!out"],  EF.IP["?sys_out"]), (proc.OP["!out"], EF.IP["?gen_pull"]), 
                  (proc.OP["!out_signal"], EF.IP["?gen_pull"]) )
   return cm

def runSimulation(md):
   engine = SimEngine(md)

   #---- setting StepByStep and DES simulation only ----
   #engine.StepByStep = True
   engine.sfactor = float('inf')
   #----------------------------------------------------
   engine.runInteractiveMenu()
   pass

############################################################################################################
if __name__ == '__main__':
   md = makeEF_ProcessNetwork()
   runSimulation(md)
 
   print "good bye~!" 
